package com.xwj.exception.solution_2.demo.controller;

import com.xwj.common.util.R;
import com.xwj.exception.common.exceptions.BusinessException;
import com.xwj.exception.solution_2.demo.entity.Student;
import com.xwj.exception.solution_2.demo.enums.BusinessResponseEnum;
import com.xwj.exception.solution_2.demo.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: xiaowajiang
 * @Date: 2022-04-05 15:49
 * @Description:
 **/
@Slf4j
@RestController
@Api(value = "StudentController", tags = "方案2 - AOP统一异常处理")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "获取学员信息", position = 1)
    @PostMapping("solution_2/business/exception/getStudent")
    public R getStudent(@RequestParam Integer id) {
        Student student = studentService.selectById(id);
        if (student == null) {
            BusinessResponseEnum responseEnum = BusinessResponseEnum.STUDENT_NOT_FOUND;
            throw new BusinessException(responseEnum.getCode(), responseEnum.getMessage());
        }
        return R.ok().put("data", student);
    }

}
